package com.frontalcode 
{
	import fl.video.*;
	import flash.display.*;
	import flash.events.*;
	
	use namespace flvplayback_internal;
	
	public class FrontalFLVPlayback extends FLVPlayback 
	{
		internal var container : ContainerVideo;
		
		public function FrontalFLVPlayback ( container : ContainerVideo )
		{
			CONFIG::FLEX
			{
				boundingBox_mc = new MovieClip ( );
			}
			
			super ( );
			this.container = container;
			
			// Remove the current uiMgr as best we can.
			//
			try
			{
				removeEventListener ( Event.ADDED_TO_STAGE, uiMgr.handleEvent );
				removeEventListener ( Event.REMOVED_FROM_STAGE, uiMgr.handleEvent );
			}
			catch ( e : * )
			{
			}
			
			uiMgr = new FrontalFLVPlaybackUIMgr ( this, container );
		}

		// When compiling with Flex, the boundingBox_mc does not exist. So we
		// create one in the constructor. We don't have a chance to add it as a
		// child so we have to make removeChild more robust to avoid an error
		// when FLVPlayback tries to remove it.
		//
		CONFIG::FLEX
		override public function removeChild ( child : DisplayObject ) : DisplayObject
		{
			if ( child != boundingBox_mc )
			{
				return super.removeChild ( child );
			}
			else
			{
				return null;
			}
		}
		
		override public function addChild ( child : DisplayObject ) : DisplayObject
		{
			if ( child === uiMgr.skin_mc && container.getStyle ( "control-attacher-index" ) != null )
			{
				// If we have an attacher, make sure the skin was attached to the
				// right place.
				//
				var attacher : Container = container.getContainerByIndex ( container.getStyle ( "control-attacher-index" ) );
				
				if ( attacher == null )
				{
					// XXX Maybe not created yet. Wait for it.
					//
				}
				else if ( uiMgr.skin_mc.parent !== attacher.movie )
				{
					if ( uiMgr.skin_mc.parent != null ) uiMgr.skin_mc.parent.removeChild ( uiMgr.skin_mc );
					attacher.movie.addChild ( uiMgr.skin_mc );
				}
			}
			else
			{
				super.addChild ( child );
			}
			
			return child; 
		}
		
		internal function addChildRaw ( child : DisplayObject ) : DisplayObject
		{
			return super.addChild ( child );
		}
		
		public function setControlEnabled ( index : int, value : Boolean ) : void
		{
			(uiMgr as FrontalFLVPlaybackUIMgr).setControlEnabled ( index, value );
		}
	}
}
